const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const app = express()
const cors = require('cors')
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json())
// app.use(cors())
app.use(express.static(path.join(__dirname,'./public')))
app.use('/node_modules',express.static(path.join(__dirname,'./node_modules')))

let whiteList = ['http://127.0.0.1:300','http://127.0.0.1:5500']
app.use(cors({origin:whiteList,methods:['get','post']}))
app.get('/gettime',(req,res)=>{
    let time = new Date()
    res.send(JSON.stringify({
        code:0,
        data:{
            time:time.getTime()
        },
        msg:'返回成功'
    }))
})
app.post('/gettime',(req,res)=>{
    let time = new Date()
    res.send(JSON.stringify({
        code:0,
        data:{
            time:time.getTime()
        },
        msg:'返回成功post'
    }))
})

app.get('/login',(req,res)=>{
    
    res.send(JSON.stringify({
        name:req.query.name,
        pwd:req.query.pwd
    }))
})

app.post('/login',(req,res)=>{
    res.send(JSON.stringify({
        name:req.body.name,
        pwd:req.body.pwd,
        msg:'这是post方式提交'
    }))
})

app.listen(3000,()=>{
    console.log(`服务已启动...`);
})